home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The X-Philes (2nd Revision)
/
The X-Philes Number 1 (1995).iso
/
xphiles
/
hp48hor1
/
ratpac.doc
< prev
next >
Wrap
Text File
|
1995-03-31
|
2KB
|
48 lines
(User.programs)
Item: 211 by _tasmith at hpcvbbs.UUCP
Author: [Ted A Smith]
Subj: Rational number package
Date: Wed Feb 06 1991 20:21
[Note: On this disk, RATPAC is a directory object, and RATPAC.LIB is a library
object. They have the same functionality. -jkh-]
Here is a rational math package:
QAdd Add two numbers
QSub Subtract
QMul Multiply
QDiv Divide
QNeg Negate
QInv Reciprocate
Q->I Convert a number to an integer
Q->F Convert a number to a continued fraction
QF-> Convert a continued fraction to a number
->NUM Convert a number to a real
CST will be a menu.
Input numbers may be real or rationals.
Rationals are represented as 'i+n/d' or 'n/d', e.g.
5 2 QDiv ==> '5/2'
'5/2' 2 QAdd ==> '4+1/2'
I took reasonable care to preserve high accuracy.
I don't have a clue what possesed me to write this.
There isn't near enough error checking on input expressions.
P.S. By truncating a continued fraction you can get the 'best'
approximation to your input number with a denominator <= the
resultant denominator. E.g.
'Pi' ->NUM ==> 3.14159265359
3.14159265359 Q->F ==> { 3 7 15 1 292 1 1 3 }
{ 3 7 15 1 292 1 1 3 } QF-> ==> 3.14159265359
{ 3 7 15 1 } QF-> ==> '355/113'
{ 3 7 } QF-> ==> '22/7'
[Note: By decrementing (not dropping) the last number in the list, you reduce
the resulting fraction by the smallest possible amount. This fact is what
makes the DEC2FRAC program work. Number theory buffs will enjoy taking
successive results from DEC2FRAC and feeding them to Ted's Q->F function.
-jkh-]